package com.rongke.web.heepay;


import com.rongke.web.heepay.util.SmallTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2017/9/8.
 * 支付通知
 * 包括绑定结果通知和支付结果通知
 */
@WebServlet(name = "ReceiveNotify")
public class ReceiveNotify extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String input = null;
        StringBuffer requestBody = new StringBuffer();
        while((input = reader.readLine()) != null) {
            requestBody.append(input);
        }
        String msg = requestBody.toString();
        System.out.println(msg);
        //返回参数进行验签
        String str = SmallTools.toJson(msg);
        Boolean isAttestationBy = SmallTools.checkSign(str,"fcec943ae103c85ceb75017925330948");

        if (isAttestationBy){
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();
            out.println("ok");
            out.flush();
            out.close();
        }else {
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();
            out.println("error");
            out.flush();
            out.close();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
